﻿using System;
using System.Linq;
using System.Web.Mvc;
using WebApp.Extensions.Logic;
using WebApp.Models;
using WebApp.Extensions.Misc;

namespace WebApp.Controllers
{
    public partial class TavoliController : Controller
    {
        public virtual ActionResult Index()
        {
            return View();
        }

        public virtual ActionResult ORD_LST()
        {
            var entities = new Entities();
            var items = entities.SET_Ordine.Where(x => x.Fine == null).OrderBy(x => x.Inizio);
            return View(items);
        }

        public virtual ActionResult ORD_CRT()
        {
            var entities = new Entities();
            var item = new SET_Ordine();
            item.Guid = Guid.NewGuid();
            item.Inizio = DateTime.Now;
            entities.SET_Ordine.Attach(item);
            entities.Entry(item).State = System.Data.EntityState.Added;
            entities.SaveChanges();
            return RedirectToAction("ORD_TAV", new { f0 = item.Guid });
        }
        /**/
        public virtual ActionResult ORD_TAV(String f0, String f1 = null, String f2 = null, String f3 = null)
        {
            var gf0 = Converter.StringToGuid(f0);
            var gf1 = Converter.StringToGuid(f1);
            var gf3 = Converter.StringToGuid(f3);
            /**/
            var entities = new Entities();
            var set_ord = entities.SET_Ordine.Single(x => x.Guid == gf0);
            if(gf1  == Guid.Empty)
            {
                gf1 = entities.ENT_Sala.OrderBy(x => x.Nome).First().Guid;  
                var query = entities.REL_Ordine_Tavolo.Where(x => x.Ordine == gf0);
                if (query.Any())
                {
                    gf1 = query.First().ENT_Tavolo.ENT_Sala.Guid;
                }
            }
            var ent_sal = entities.ENT_Sala.Single(x => x.Guid == gf1);
            ViewBag.SET_Ordine = set_ord;
            ViewBag.ENT_Sala = ent_sal;
            if (String.IsNullOrWhiteSpace(f2) == false && String.IsNullOrWhiteSpace(f3) == false)
            {
                switch (f2)
                {
                    case "SET":
                        set_ord.TAV_Set(gf3);
                        break;
                    case "DEL":
                        set_ord.TAV_Del(gf3);
                        break;
                }
            }
            return View(set_ord.TAV_Get(gf1));
        }
        /* Visualizza la lista delle comande*/
        public virtual ActionResult ORD_CMD(String f0)
        {
            var gf0 = Converter.StringToGuid(f0);
            var entities = new Entities();
            var set_ord = entities.SET_Ordine.Where(x => x.Guid == gf0).Single();
            ViewBag.SET_Ordine = set_ord;
            var query = entities.REL_Ordine_Variante.Where(x => x.Ordine == gf0);
            return View(query);
        }
        /*Visualizza il menu e consente di inserire una nuova comanda*/
        public virtual ActionResult ORD_CMD_SEL(String f0, String f1 = null)
        {
            var gf0 = Converter.StringToGuid(f0);
            var gf1 = Converter.StringToGuid(f1);
            var entities = new Entities();
            if (gf1 == Guid.Empty)
            {
                gf1 = entities.ENT_Gruppo.OrderBy(x => x.Nome).First().Guid;
            }
            var set_ord = entities.SET_Ordine.Single(x => x.Guid == gf0);
            ViewBag.SET_Ordine = set_ord;
            var query = entities.ENT_Piatto.Where(x => x.Famiglia == gf1).OrderBy(x => x.Nome);
            return View(query);
        }
        /*Aggiunge la comanda all'ordine*/
        public virtual ActionResult ORD_CMD_ADD(String f0, String f1)
        {
            var gf0 = Converter.StringToGuid(f0);
            var gf1 = Converter.StringToGuid(f1);
            var entities = new Entities();
            var set_ord = entities.SET_Ordine.Single(x => x.Guid == gf0);
            var ent_pia = entities.ENT_Piatto.Single(x => x.Guid == gf1);
            var ord_var = new REL_Ordine_Variante();
            ord_var.Ordine = gf0;
            ord_var.Piatto = gf1;
            ord_var.Guid = Guid.NewGuid();
            entities.Entry(ord_var).State = System.Data.EntityState.Added;
            entities.SaveChanges();
            ent_pia.REL_Piatto_Ingrediente.ToList().ForEach(x =>
                                                            {
                                                                var set_var = new SET_Variante();
                                                                set_var.Guid = Guid.NewGuid();
                                                                set_var.Ingrediente = x.Ingrediente;
                                                                set_var.Variante = ord_var.Guid;
                                                                entities.Entry(set_var).State = System.Data.EntityState.Added;
                                                                entities.SaveChanges();
                                                            });
            return RedirectToAction("ORD_CMD_EDT", "Tavoli", new { f0 = f0, f1 = ord_var.Guid });
        }
        /*Modifica la comanda*/
        public virtual ActionResult ORD_CMD_EDT(String f0, String f1, String f2, String f3)
        {
            var gf0 = Converter.StringToGuid(f0);
            var gf1 = Converter.StringToGuid(f1);
            var gf3 = Converter.StringToGuid(f3);
            var entities = new Entities();
            var set_ord = entities.SET_Ordine.Where(x => x.Guid == gf0).Single();
            var rel_ord_var = entities.REL_Ordine_Variante.Where(x => x.Guid == gf1).Single();
            if (String.IsNullOrWhiteSpace(f2) == false && String.IsNullOrWhiteSpace(f3) == false)
            {
                switch (f2)
                {
                    case "SET":
                        rel_ord_var.ING_Set(gf3);
                        break;
                    case "DEL":
                        rel_ord_var.ING_Del(gf3);
                        break;
                }
            }
            ViewBag.SET_Ordine = set_ord;
            ViewBag.REL_Ordine_Variante = rel_ord_var;
            return View(rel_ord_var.ING_Get());
        }
    }
}
